#include <allegro5/allegro.h>
#include <allegro5/allegro_primitives.h>  

#include "data_struct_game.h"
#include "blockOperation.h"
#include "drawBlock.h"

extern block_type field[height_matrix][width_matrix];

int MoveRight(pivot block){
  if (MoveLeftRight(block, true)){
    int right_margin;
    short int actual_x = block.x;
    int rotation = block.rotation;
    switch (block.type){
      case I:
        if (((rotation % 2) + 1) == 2)
          right_margin = 9;
        else
          right_margin = 7;
        break;
      case T:
      case J:    
      case L:
        if (((rotation % 4) + 1) == 4)
          right_margin = 9;
        else
          right_margin = 8;
        break;
      case Z:
      case O:
        right_margin = 8;
        break;
      case S:
        if (((rotation % 2) + 1) == 2)
          right_margin = 9;
        else
          right_margin = 8;
        break;
      default:
        break;
    }
    if ((actual_x + 1) > right_margin)
      return right_margin;
    else
      return actual_x += 1;
  }
  else return block.x;
}

int MoveLeft(pivot block){
  if (MoveLeftRight(block, false)){
    int left_margin;
    short int actual_x = block.x;
    int rotation = block.rotation;
    
    switch (block.type){
      case I:
      case Z:
        if (((rotation % 2) + 1) == 2)
          left_margin = 0;
        else
          left_margin = 1;
        break;
      case T:
      case J:
      case L:
        if (((rotation % 4) + 1) == 2)
          left_margin = 0;
        else
          left_margin = 1;
        break;
      case O:
        left_margin = 0;
        break;
      case S:
        left_margin = 1;
         break;
      default:
        break;
    }
    if ((actual_x - 1) < left_margin)
      return left_margin;
    else
      return actual_x -= 1;
    }
  else return block.x;
}

bool MoveLeftRight(pivot block, bool right){
  short int shift;
  if (right)
    shift = 1;
  else
    shift = -1;
  switch (block.type){
    case I:
      return CanMoveRotateI(block, true, shift);
    case J:
      return CanMoveRotateJ(block, true, shift);
    case L:
      return CanMoveRotateL(block, true, shift);
    case O:
      return MoveLeftRightO(block, shift);
    case S:
      return CanMoveRotateS(block, true, shift);
    case Z:
      return CanMoveRotateZ(block, true, shift);
    case T:
      return CanMoveRotateT(block, true, shift);
    default:
      return false;
      break;
  }
}

int ControlLeftRightX(short int &pivot_x, block_type actual_type){
  if (pivot_x == 0)
    return pivot_x = 1;
  else if ((pivot_x == 9) && actual_type == I)
    return pivot_x = 7;
  else if (pivot_x == 9)
    return pivot_x = 8;
  else return pivot_x;
}

bool ControlY(short int pivot_y, int shift_down){
  if (pivot_y + shift_down <= 19)
    return true;
  else
     return false;
}

bool CanMoveRotateI(pivot &block, bool move, short int shift){
  short int pivot_x = block.x;
  short int pivot_y = block.y;
  if (!move)
    block.x = ControlLeftRightX(pivot_x, I);
  else
    pivot_x += shift;
  switch ((block.rotation % 2) + 1){
    case 1:
      if ((field[pivot_y][pivot_x - 1] == N) &&
          (field[pivot_y][pivot_x] == N) &&
          (field[pivot_y][pivot_x + 1] == N) &&
          (field[pivot_y][pivot_x + 2] == N))
        return true;
      else
        return false;
    case 2:
      if ((ControlY(pivot_y, 3) || move) &&
          (field[pivot_y - 1][pivot_x] == N) &&
          (field[pivot_y][pivot_x] == N) &&
          (field[pivot_y + 1][pivot_x] == N) &&
          (field[pivot_y + 2][pivot_x] == N))
        return true;
      else
        return false;
    default:
      return false;
      break;
  }
}

bool CanMoveRotateJ(pivot &block, bool move, short int shift){
  short int pivot_x = block.x;
  short int pivot_y = block.y;
  if (!move)
    block.x = ControlLeftRightX(pivot_x, J);
  else
    pivot_x += shift;
  switch ((block.rotation % 4) + 1){
    case 1:
      if ((field[pivot_y - 1][pivot_x - 1] == N) &&
          (field[pivot_y][pivot_x - 1] == N) &&
          (field[pivot_y][pivot_x] == N) &&
          (field[pivot_y][pivot_x + 1] == N))
        return true;
      else
        return false;
    case 2:
      if ((ControlY(pivot_y, 1) || move) &&
          (field[pivot_y - 1][pivot_x] == N) &&
          (field[pivot_y - 1][pivot_x + 1] == N) &&
          (field[pivot_y][pivot_x] == N) &&
          (field[pivot_y + 1][pivot_x] == N))
        return true;
      else
        return false;
    case 3:
      if ((field[pivot_y][pivot_x - 1] == N) &&
          (field[pivot_y][pivot_x] == N) &&
          (field[pivot_y][pivot_x + 1] == N) &&
          (field[pivot_y + 1][pivot_x + 1] == N))
        return true;
      else
        return false;
    case 4:
      if ((field[pivot_y + 1][pivot_x - 1] == N) &&
          (field[pivot_y + 1][pivot_x] == N) &&
          (field[pivot_y][pivot_x] == N) &&
          (field[pivot_y - 1][pivot_x] == N))
        return true;
      else
        return false;
    default:
      return false;
      break;
  }
}

bool CanMoveRotateL(pivot &block, bool move, short int shift){
  short int pivot_x = block.x;
  short int pivot_y = block.y;
  if (!move)
    block.x = ControlLeftRightX(pivot_x, J);
  else
    pivot_x += shift;
  switch ((block.rotation % 4) + 1){
    case 1:
      if ((field[pivot_y][pivot_x - 1] == N) &&
          (field[pivot_y][pivot_x] == N) &&
          (field[pivot_y][pivot_x + 1] == N) &&
          (field[pivot_y - 1][pivot_x + 1] == N))
        return true;
      else
        return false;
    case 2:
      if ((ControlY(pivot_y, 1) || move) &&
          (field[pivot_y - 1][pivot_x] == N) &&
          (field[pivot_y][pivot_x] == N) &&
          (field[pivot_y + 1][pivot_x] == N) &&
          (field[pivot_y + 1][pivot_x + 1] == N))
        return true;
      else
        return false;
    case 3:
      if ((field[pivot_y + 1][pivot_x - 1] == N) &&
          (field[pivot_y][pivot_x - 1] == N) &&
          (field[pivot_y][pivot_x] == N) &&
          (field[pivot_y][pivot_x + 1] == N))
        return true;
      else
        return false;
    case 4:
      if ((field[pivot_y - 1][pivot_x - 1] == N) &&
          (field[pivot_y - 1][pivot_x] == N) &&
          (field[pivot_y][pivot_x] == N) &&
          (field[pivot_y + 1][pivot_x] == N))
        return true;
      else
        return false;
    default:
      return false;
      break;
  }
}

bool MoveLeftRightO(pivot block, short int shift){
  short int pivot_x = block.x + shift;
  short int pivot_y = block.y;
  if ((field[pivot_y][pivot_x] == N) &&
      (field[pivot_y][pivot_x + 1] == N) &&
      (field[pivot_y + 1][pivot_x] == N) &&
      (field[pivot_y + 1][pivot_x + 1] == N))
    return true;
  else
    return false;
}

bool CanMoveRotateS(pivot &block, bool move, short int shift){
  short int pivot_x = block.x;
  short int pivot_y = block.y;
  if (!move)
    block.x = ControlLeftRightX(pivot_x, S);
  else
    pivot_x += shift;
  switch ((block.rotation % 2) + 1){
    case 1:
      if ((field[pivot_y][pivot_x - 1] == N) &&
          (field[pivot_y][pivot_x] == N) &&
          (field[pivot_y - 1][pivot_x] == N) &&
          (field[pivot_y - 1][pivot_x + 1] == N))
        return true;
      else
        return false;
    case 2:
      if ((ControlY(pivot_y, 1) || move) &&
          (field[pivot_y - 1][pivot_x - 1] == N) &&
          (field[pivot_y][pivot_x - 1] == N) &&
          (field[pivot_y][pivot_x] == N) &&
          (field[pivot_y + 1][pivot_x] == N))
        return true;
      else
        return false;
    default:
      return false;
      break;
  }
}

bool CanMoveRotateZ(pivot &block, bool move, short int shift){
  short int pivot_x = block.x;
  short int pivot_y = block.y;
  if (!move)
    block.x = ControlLeftRightX(pivot_x, Z);
  else
    pivot_x += shift;
  switch ((block.rotation % 2) + 1){
    case 1:
      if ((field[pivot_y - 1][pivot_x - 1] == N) &&
          (field[pivot_y - 1][pivot_x] == N) &&
          (field[pivot_y][pivot_x] == N) &&
          (field[pivot_y][pivot_x + 1] == N))
        return true;
      else
        return false;
    case 2:
      if ((ControlY(pivot_y, 1) || move) &&
          (field[pivot_y + 1][pivot_x] == N) &&
          (field[pivot_y][pivot_x] == N) &&
          (field[pivot_y][pivot_x + 1] == N) &&
          (field[pivot_y - 1][pivot_x + 1] == N))
       return true;
      else
       return false;
    default:
      return false;
      break;
 }
}

bool CanMoveRotateT(pivot &block, bool move, short int shift){
  short int pivot_x = block.x;
  short int pivot_y = block.y;
  if (!move)
    block.x = ControlLeftRightX(pivot_x, T);
  else
    pivot_x += shift;
  switch ((block.rotation % 4) + 1){
    case 1:
      if (pivot_x == 9)
        pivot_x = 8;
      if ((field[pivot_y][pivot_x - 1] == N) &&
          (field[pivot_y][pivot_x] == N) &&
          (field[pivot_y - 1][pivot_x] == N) &&
          (field[pivot_y][pivot_x + 1] == N))
        return true;
      else
        return false;
    case 2:
      if ((ControlY(pivot_y, 1)) &&
          (field[pivot_y - 1][pivot_x] == N) &&
          (field[pivot_y][pivot_x] == N) &&
          (field[pivot_y][pivot_x + 1] == N) &&
          (field[pivot_y + 1][pivot_x] == N))
        return true;
      else
        return false;
    case 3:
      if (pivot_x == 0)
        pivot_x = 1;
      if ((field[pivot_y][pivot_x - 1] == N) &&
          (field[pivot_y][pivot_x] == N) &&
          (field[pivot_y + 1][pivot_x] == N) &&
          (field[pivot_y][pivot_x + 1] == N))
        return true;
      else
        return false;
    case 4:
      if ((field[pivot_y][pivot_x - 1] == N) &&
          (field[pivot_y - 1][pivot_x] == N) &&
          (field[pivot_y][pivot_x] == N) &&
          (field[pivot_y + 1][pivot_x] == N))
        return true;
      else
        return false;
    default:
      return false;
      break;
  }
}

void RotationI(pivot block){
  short int pivot_x = block.x;
  short int pivot_y = block.y;
  switch ((block.rotation % 2) + 1){
    case 1:
      DrawLeft(pivot_x, pivot_y, 0, 255, 255);
      DrawRight(pivot_x, pivot_y, 0, 255, 255);
      DrawRight(pivot_x + 1, pivot_y, 0, 255, 255);
      break;
    case 2:
      DrawUp(pivot_x, pivot_y, 0, 255, 255);
      DrawDown(pivot_x, pivot_y, 0, 255, 255);
      DrawDown(pivot_x, pivot_y + 1, 0, 255, 255);
      break;
    default:
      break;
  }
  DrawPivot(pivot_x, pivot_y, 0, 255, 255);
}

void RotationJ(pivot block){
  short int pivot_x = block.x;
  short int pivot_y = block.y;
  switch ((block.rotation % 4) + 1){
    case 1:
      DrawUpLeft(pivot_x, pivot_y, 0, 0, 255);
      DrawLeft(pivot_x, pivot_y, 0, 0, 255);
      DrawRight(pivot_x, pivot_y, 0, 0, 255);
      break;
    case 2:
      DrawUp(pivot_x, pivot_y, 0, 0, 255);
      DrawUpRight(pivot_x, pivot_y, 0, 0, 255);
      DrawDown(pivot_x, pivot_y, 0, 0, 255);
      break;
    case 3:
      DrawLeft(pivot_x, pivot_y, 0, 0, 255);
      DrawRight(pivot_x, pivot_y, 0, 0, 255);
      DrawDownRight(pivot_x, pivot_y, 0, 0, 255);
      break;
    case 4:
      DrawDown(pivot_x, pivot_y, 0, 0, 255);
      DrawDownLeft(pivot_x, pivot_y, 0, 0, 255);
      DrawUp(pivot_x, pivot_y, 0, 0, 255);
      break;
     default:
      break;
  }
  DrawPivot(pivot_x, pivot_y, 0, 0, 255);
}

void RotationL(pivot block){
  short int pivot_x = block.x;
  short int pivot_y = block.y;
  switch ((block.rotation % 4) + 1){
    case 1:
      DrawLeft(pivot_x, pivot_y, 255, 102, 0);
      DrawRight(pivot_x, pivot_y, 255, 102, 0);
      DrawUpRight(pivot_x, pivot_y, 255, 102, 0);
      break;
    case 2:
      DrawDown(pivot_x, pivot_y, 255, 102, 0);
      DrawDownRight(pivot_x, pivot_y, 255, 102, 0);
      DrawUp(pivot_x, pivot_y, 255, 102, 0);
      break;
    case 3:
      DrawLeft(pivot_x, pivot_y, 255, 102, 0);
      DrawDownLeft(pivot_x, pivot_y, 255, 102, 0);
      DrawRight(pivot_x, pivot_y, 255, 102, 0);
      break;
    case 4:
      DrawUpLeft(pivot_x, pivot_y, 255, 102, 0);
      DrawUp(pivot_x, pivot_y, 255, 102, 0);
      DrawDown(pivot_x, pivot_y, 255, 102, 0);
      break;
    default:
      break;
  }
  DrawPivot(pivot_x, pivot_y, 255, 102, 0);
}

void RotationO(pivot block){
  short int pivot_x = block.x;
  short int pivot_y = block.y;
  DrawPivot(pivot_x, pivot_y, 255, 255, 0);
  DrawRight(pivot_x, pivot_y, 255, 255, 0);
  DrawDown(pivot_x, pivot_y, 255, 255, 0);
  DrawDownRight(pivot_x, pivot_y, 255, 255, 0);
}

void RotationS(pivot block){
  short int pivot_x = block.x;
  short int pivot_y = block.y;
  switch ((block.rotation % 2) + 1){
    case 1:
      DrawLeft(pivot_x, pivot_y, 102, 255, 0);
      DrawUp(pivot_x, pivot_y, 102, 255, 0);
      DrawUpRight(pivot_x, pivot_y, 102, 255, 0);
      break;
    case 2:
      DrawUpLeft(pivot_x, pivot_y, 102, 255, 0);
      DrawLeft(pivot_x, pivot_y, 102, 255, 0);
      DrawDown(pivot_x, pivot_y, 102, 255, 0);
      break;
    default:
      break;
  }    
  DrawPivot(pivot_x, pivot_y, 102, 255, 0);
}

void RotationZ(pivot block){
  short int pivot_x = block.x;
  short int pivot_y = block.y;
  switch ((block.rotation % 2) + 1){
    case 1:
      DrawUpLeft(pivot_x, pivot_y, 255, 0, 0);
      DrawUp(pivot_x, pivot_y, 255, 0, 0);
      DrawRight(pivot_x, pivot_y, 255, 0, 0);
      break;
    case 2:
      DrawDown(pivot_x, pivot_y, 255, 0, 0);
      DrawRight(pivot_x, pivot_y, 255, 0, 0);
      DrawUpRight(pivot_x, pivot_y, 255, 0, 0);
      break;
    default:
      break;
  }
  DrawPivot(pivot_x, pivot_y, 255, 0, 0);
}

void RotationT(pivot block){
  short int pivot_x = block.x;
  short int pivot_y = block.y;
  switch ((block.rotation % 4) + 1){
    case 1:
      DrawLeft(pivot_x, pivot_y, 143, 0, 255);
      DrawUp(pivot_x, pivot_y, 143, 0, 255);
      DrawRight(pivot_x, pivot_y, 143, 0, 255);
      break;
    case 2:
      DrawUp(pivot_x, pivot_y, 143, 0, 255);
      DrawDown(pivot_x, pivot_y, 143, 0, 255);
      DrawRight(pivot_x, pivot_y, 143, 0, 255);
      break;
    case 3:
      DrawLeft(pivot_x, pivot_y, 143, 0, 255);
      DrawDown(pivot_x, pivot_y, 143, 0, 255);
      DrawRight(pivot_x, pivot_y, 143, 0, 255);
      break;
    case 4:
      DrawLeft(pivot_x, pivot_y, 143, 0, 255);
      DrawUp(pivot_x, pivot_y, 143, 0, 255);
      DrawDown(pivot_x, pivot_y, 143, 0, 255);
      break;
    default:
      break;
  }
  DrawPivot(pivot_x, pivot_y, 143, 0, 255);
}

bool MoveDownI(pivot block){
  short int pivot_x = block.x;
  short int pivot_y = block.y;
  switch ((block.rotation % 2) + 1){
    case 1:
      if (((pivot_y + 1) <= 19) &&
          (field[pivot_y + 1][pivot_x - 1] == N) &&
          (field[pivot_y + 1][pivot_x] == N) &&
          (field[pivot_y + 1][pivot_x + 1] == N) &&
          (field[pivot_y + 1][pivot_x + 2] == N))
        return true;
      else
        return false;
      break;
    case 2:
      if (((pivot_y + 3) <= 19) &&
          (field[pivot_y + 3][pivot_x] == N))
        return true;
      else
        return false;
      break;
    default:
      return false;
      break;
  }
}

bool MoveDownJ(pivot block){
  short int pivot_x = block.x;
  short int pivot_y = block.y;
  switch ((block.rotation % 4) + 1){
    case 1:
      if (((pivot_y + 1) <= 19) &&
          (field[pivot_y + 1][pivot_x - 1] == N) &&
          (field[pivot_y + 1][pivot_x] == N) &&
          (field[pivot_y + 1][pivot_x + 1] == N))
        return true;
      else
        return false;
      break;
    case 2:
      if (((pivot_y + 2) <= 19) &&
          (field[pivot_y + 2][pivot_x] == N) &&
          (field[pivot_y][pivot_x + 1] == N))
        return true;
      else
        return false;
      break;
    case 3:
      if (((pivot_y + 2) <= 19) &&
          (field[pivot_y + 1][pivot_x - 1] == N) &&
          (field[pivot_y + 1][pivot_x] == N) &&
          (field[pivot_y + 2][pivot_x + 1] == N))
        return true;
      else
        return false;
      break;
    case 4:
      if (((pivot_y + 2) <= 19) &&
          (field[pivot_y + 2][pivot_x - 1] == N) &&
          (field[pivot_y + 2][pivot_x] == N))
        return true;
      else
        return false;
      break;
    default:
      return false;
      break;
  }
}

bool MoveDownL(pivot block){
  short int pivot_x = block.x;
  short int pivot_y = block.y;
  switch ((block.rotation % 4) + 1){
    case 1:
      if (((pivot_y + 1) <= 19) &&
          (field[pivot_y + 1][pivot_x - 1] == N) &&
          (field[pivot_y + 1][pivot_x] == N) &&
          (field[pivot_y + 1][pivot_x + 1] == N))
        return true;
      else
        return false;
      break;
    case 2:
      if (((pivot_y + 2) <= 19) &&
          (field[pivot_y + 2][pivot_x] == N) &&
          (field[pivot_y + 2][pivot_x + 1] == N))
        return true;
      else
        return false;
      break;
    case 3:
      if (((pivot_y + 2) <= 19) &&
          (field[pivot_y + 2][pivot_x - 1] == N) &&
          (field[pivot_y + 1][pivot_x] == N) &&
          (field[pivot_y + 1][pivot_x + 1] == N))
        return true;
      else
        return false;
      break;
    case 4:
      if (((pivot_y + 2) <= 19) &&
          (field[pivot_y + 2][pivot_x] == N) &&
          (field[pivot_y][pivot_x - 1] == N))
        return true;
      else
        return false;
      break;
    default:
      return false;
      break;
  }   
}

bool MoveDownO(pivot block){
  short int pivot_x = block.x;
  short int pivot_y = block.y;
  if (((pivot_y + 2) <= 19) &&
      (field[pivot_y + 2][pivot_x] == N) &&
      (field[pivot_y + 2][pivot_x + 1] == N))
    return true;
  else
    return false;
}

bool MoveDownS(pivot block){
  short int pivot_x = block.x;
  short int pivot_y = block.y;
  switch ((block.rotation % 2) + 1){
    case 1:
      if (((pivot_y + 1) <= 19) && 
          (field[pivot_y + 1][pivot_x - 1] == N) &&
          (field[pivot_y + 1][pivot_x] == N) &&
          (field[pivot_y][pivot_x + 1] == N))
        return true;
      else
        return false;
      break;
    case 2:
      if (((pivot_y + 2) <= 19) && 
          (field[pivot_y + 1][pivot_x - 1] == N) &&
          (field[pivot_y + 2][pivot_x] == N))
        return true;
      else
        return false;
      break;
    default:
      return false;
      break;
  }
}

bool MoveDownZ(pivot block){
  short int pivot_x = block.x;
  short int pivot_y = block.y;
  switch ((block.rotation % 2) + 1){
    case 1:
      if (((pivot_y + 1) <= 19) &&
          (field[pivot_y][pivot_x - 1] == N) &&
          (field[pivot_y + 1][pivot_x] == N) &&
          (field[pivot_y + 1][pivot_x + 1] == N))
        return true;
      else
        return false;
      break;
    case 2:
      if (((pivot_y + 2) <= 19) &&
          (field[pivot_y + 2][pivot_x] == N) &&
          (field[pivot_y + 1][pivot_x + 1] == N))
        return true;
      else
        return false;
      break;
    default:
      return false;
      break;
  }
}

bool MoveDownT(pivot block){
  short int pivot_x = block.x;
  short int pivot_y = block.y;
  switch ((block.rotation % 4) + 1){
    case 1:
      if (((pivot_y + 1) <= 19) &&
          (field[pivot_y + 1][pivot_x - 1] == N) &&
          (field[pivot_y + 1][pivot_x] == N) &&
          (field[pivot_y + 1][pivot_x + 1] == N))
        return true;
      else
        return false;
      break;
    case 2:
      if (((pivot_y + 2) <= 19) &&
          (field[pivot_y + 2][pivot_x] == N) &&
          (field[pivot_y + 1][pivot_x + 1] == N))
        return true;
      else
        return false;
      break;
    case 3:
      if (((pivot_y + 2) <= 19) &&
          (field[pivot_y + 1][pivot_x - 1] == N) &&
          (field[pivot_y + 2][pivot_x] == N) &&
          (field[pivot_y + 1][pivot_x + 1] == N))
        return true;
      else
        return false;
      break;
    case 4:
      if (((pivot_y + 2) <= 19) &&
          (field[pivot_y + 1][pivot_x - 1] == N) &&
          (field[pivot_y + 2][pivot_x] == N))
        return true;
      else
        return false;
      break;
    default:
      return false;	
      break;
  }  
}

void BlockI(pivot block){
  short int pivot_x = block.x;
  short int pivot_y = block.y;
  switch ((block.rotation % 2) + 1){
    case 1:
      field[pivot_y][pivot_x - 1] = I;
      field[pivot_y][pivot_x + 1] = I;
      field[pivot_y][pivot_x + 2] = I;
      break;
    case 2:
      field[pivot_y - 1][pivot_x] = I;
      field[pivot_y + 1][pivot_x] = I;
      field[pivot_y + 2][pivot_x] = I;
      break;
    default:
      break;
  }
  field[pivot_y][pivot_x] = I;
}

void BlockJ(pivot block){
  short int pivot_x = block.x;
  short int pivot_y = block.y;
  switch ((block.rotation % 4) + 1){
    case 1:
      field[pivot_y - 1][pivot_x - 1] = J;
      field[pivot_y][pivot_x - 1] = J;
      field[pivot_y][pivot_x + 1] = J;
      break;
    case 2:
      field[pivot_y - 1][pivot_x] = J;
      field[pivot_y - 1][pivot_x + 1] = J;
      field[pivot_y + 1][pivot_x] = J;
      break;
    case 3:
      field[pivot_y][pivot_x - 1] = J;
      field[pivot_y][pivot_x + 1] = J;
      field[pivot_y + 1][pivot_x + 1] = J;
      break;
    case 4:
      field[pivot_y + 1][pivot_x - 1] = J;
      field[pivot_y + 1][pivot_x] = J;
      field[pivot_y - 1][pivot_x] = J;
      break;
    default:
      break;
  }
  field[pivot_y][pivot_x] = J;
}

void BlockL(pivot block){
  short int pivot_x = block.x;
  short int pivot_y = block.y;
  switch ((block.rotation % 4) + 1){
    case 1:
      field[pivot_y][pivot_x - 1] = L;
      field[pivot_y][pivot_x + 1] = L;
      field[pivot_y - 1][pivot_x + 1] = L;
      break;
    case 2:
      field[pivot_y - 1][pivot_x] = L;
      field[pivot_y + 1][pivot_x] = L;
      field[pivot_y + 1][pivot_x + 1] = L;
      break;
    case 3:
      field[pivot_y + 1][pivot_x - 1] = L;
      field[pivot_y][pivot_x - 1] = L;
      field[pivot_y][pivot_x + 1] = L;
      break;
    case 4:
      field[pivot_y - 1][pivot_x - 1] = L;
      field[pivot_y - 1][pivot_x] = L;
      field[pivot_y + 1][pivot_x] = L;
      break;
    default:
      break;
  }
  field[pivot_y][pivot_x] = L;
}

void BlockO(pivot block){
  short int pivot_x = block.x;
  short int pivot_y = block.y;
  field[pivot_y][pivot_x] = O;
  field[pivot_y + 1][pivot_x] = O;
  field[pivot_y][pivot_x + 1] = O;
  field[pivot_y + 1][pivot_x + 1] = O;
}

void BlockS(pivot block){
  short int pivot_x = block.x;
  short int pivot_y = block.y;
  switch ((block.rotation % 2) + 1){
    case 1:
      field[pivot_y][pivot_x - 1] = S;
      field[pivot_y - 1][pivot_x] = S;
      field[pivot_y - 1][pivot_x + 1] = S;
      break;
    case 2:
      field[pivot_y - 1][pivot_x - 1] = S;
      field[pivot_y][pivot_x - 1] = S;
      field[pivot_y + 1][pivot_x] = S;
      break;
    default:
      break;
  }
  field[pivot_y][pivot_x] = S;
}

void BlockZ(pivot block){
  short int pivot_x = block.x;
  short int pivot_y = block.y;
  switch ((block.rotation % 2) + 1){
    case 1:
      field[pivot_y - 1][pivot_x - 1] = Z;
      field[pivot_y - 1][pivot_x] = Z;
      field[pivot_y][pivot_x + 1] = Z;
      break;
    case 2:
      field[pivot_y + 1][pivot_x] = Z;
      field[pivot_y][pivot_x + 1] = Z;
      field[pivot_y - 1][pivot_x + 1] = Z;
      break;
    default:
      break;
  }
  field[pivot_y][pivot_x] = Z;
}

void BlockT(pivot block){
  short int pivot_x = block.x;
  short int pivot_y = block.y;
  switch ((block.rotation % 4) + 1){
    case 1:
      field[pivot_y][pivot_x - 1] = T;
      field[pivot_y - 1][pivot_x] = T;
      field[pivot_y][pivot_x + 1] = T;
      break;
    case 2:
      field[pivot_y - 1][pivot_x] = T;
      field[pivot_y][pivot_x + 1] = T;
      field[pivot_y + 1][pivot_x] = T;
      break;
    case 3:
      field[pivot_y][pivot_x - 1] = T;
      field[pivot_y + 1][pivot_x] = T;
      field[pivot_y][pivot_x + 1] = T;
      break;
    case 4:
      field[pivot_y][pivot_x - 1] = T;
      field[pivot_y - 1][pivot_x] = T;
      field[pivot_y + 1][pivot_x] = T;
      break;
    default:
      break;
  }
  field[pivot_y][pivot_x] = T;
}
